﻿// FIV Fast Image viewer
// http://code.google.com/p/fiv/
// 
// Copyright 2013, Ken Rentz.
//
// This is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation; either version 2.1 of
// the License, or (at your option) any later version.
//
// This software is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this software; if not, write to the Free
// Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
// 02110-1301 USA, or see the FSF site: http://www.fsf.org.
// 


using System;
using System.Drawing;
using System.IO;

namespace FIV
{
	class BaseImage: IDisposable
	{
		private Bitmap _DisplayedImage;
		private Bitmap _RawImage;
		private Size _SizeToDisplay;

		public Size ActualImageSize
		{
			get { return _RawImage == null ? Size.Empty : _RawImage.Size; }
		}

		public string Filename { get; private set; }

		public void AdjustDisplayedImageSize(Size size)
		{
			if (Options.Instance.MaintainAspectRatio)
				_SizeToDisplay = AdjustSizeMaintainingAspectRatio(size);
			else
				_SizeToDisplay = size;
		}

		public void Dispose()
		{
			_RawImage.Dispose();
			_DisplayedImage.Dispose();
		}

		public string GetDescriptiveText()
		{
			// TODO make the format more user friendly
			// TODO internationalize DPI
			// TODO add file format
			// TODO error checking
			string text = String.Format(" - [{0}] ({1} x {2}, {3}  {4}x{5}dpi)", Filename, _RawImage.Width, _RawImage.Height, _RawImage.PixelFormat, _RawImage.HorizontalResolution, _RawImage.VerticalResolution);
			return text;
		}

		public Bitmap GetImage(Size windowSize)
		{
			if (_RawImage != null && _SizeToDisplay != Size.Empty)
				_DisplayedImage = new Bitmap(_RawImage, _SizeToDisplay);
			return _DisplayedImage;
		}

		public bool Load(string fileName)
		{
			Filename = Path.GetFileName(fileName);
			_DisplayedImage = null;

			bool rc = true;
			try
			{
				_RawImage = new Bitmap(fileName);
				_SizeToDisplay = _RawImage.Size;
			}

			catch (Exception)
			{
				_SizeToDisplay = Size.Empty;
				rc = false;
			}

			_DisplayedImage = _RawImage;
			return rc;
		}

		private Size AdjustSizeMaintainingAspectRatio(Size windowSize)
		{
			if(_RawImage == null || _RawImage.Width == 0 || _RawImage.Height == 0 || windowSize.Height == 0)
				return windowSize;

			double screenRatio = windowSize.Width / (double)windowSize.Height;
			double imageRatio = _RawImage.Width / (double)_RawImage.Height;

			double scale;
			if (imageRatio > screenRatio) // scale to height
			{
				scale = (double)windowSize.Width / _RawImage.Width;
			}
			else // scale to width
			{
				scale = (double) windowSize.Height / _RawImage.Height;
			}

			return new Size((int)(_RawImage.Width * scale), (int)(_RawImage.Height * scale));		
		}
	}

}
